package com.samapp.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.samapp.MyApplication;
import com.samapp.entity.Client;

public class SamappDB extends SQLiteOpenHelper {

	public SamappDB(Context context, int version) {
		super(context, MyApplication.getApplicationInstance().databaseName, null, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.i("Long", "创建表");
		db.execSQL(MyApplication.getApplicationInstance().createTable);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String sql = "drop table if exists " + MyApplication.getApplicationInstance().tableName;
		Log.i("Long", "删除表再创建表");
		db.execSQL(sql);
		onCreate(db);
	}

	// 查询所有的数据
	public Cursor select() {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(MyApplication.getApplicationInstance().tableName, null, null, null, null, null, null);
		return cursor;
	}

	// 根据fid查询
	public Cursor selectByFid(String fid) {
		SQLiteDatabase db = this.getReadableDatabase();
		String where = fid + "= ?";
		String[] wherevalues = { fid };
		Cursor cursor = db.query(MyApplication.getApplicationInstance().tableName, null, where, wherevalues, null, null, null);
		return cursor;
	}

	// 添加一条记录
	public long insert(String fid, String cname, String ename, String number, String type, String ccontacts, String econtacts, String phone, String mobile, String fax, String email, String addresscn, String addressen, String remark, String longitude, String latitude) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("fid", fid);
		cv.put("cname", cname);
		cv.put("ename", ename);
		cv.put("number", number);
		cv.put("type", type);
		cv.put("ccontacts", ccontacts);
		cv.put("econtacts", econtacts);
		cv.put("phone", phone);
		cv.put("mobile", mobile);
		cv.put("fax", fax);
		cv.put("email", email);
		cv.put("addressCn", addresscn);
		cv.put("addressEn", addressen);
		cv.put("remark", remark);
		cv.put("longitude", remark);
		cv.put("latitude", latitude);
		long result = db.insert(MyApplication.getApplicationInstance().tableName, null, cv);
		return result;
	}

	/**
	 * 根据对象添加一条记录
	 * 
	 * @param client
	 * @return
	 */
	public long insertByObject(Client client) {
		if (client == null) {
			return -1;
		} else {
			SQLiteDatabase db = this.getWritableDatabase();
			ContentValues cv = new ContentValues();
			cv.put("fid", client.getFid());
			Log.i("Long", "fid:" + client.getFid());
			cv.put("cname", client.getCname());
			cv.put("ename", client.getEname());
			cv.put("number", client.getNumber());
			cv.put("type", client.getType());
			cv.put("ccontacts", client.getCcontacts());
			cv.put("econtacts", client.getEcontacts());
			cv.put("phone", client.getPhone());
			cv.put("mobile", client.getMobile());
			cv.put("fax", client.getFax());
			cv.put("email", client.getEmail());
			cv.put("addresscn", client.getAddresscn());
			cv.put("addressen", client.getAddressen());
			cv.put("remark", client.getRemark());
			cv.put("longitude", client.getLongitude());
			cv.put("latitude", client.getLatitude());
			long result = db.insert(MyApplication.getApplicationInstance().tableName, null, cv);
			return result;
		}
	}

	// 删除操作
	public void delete(String fid) {
		SQLiteDatabase db = this.getWritableDatabase();
		String where = fid + " = ?";
		String[] wherevalues = { fid };
		db.delete(MyApplication.getApplicationInstance().tableName, where, wherevalues);
	}

	public void update(String fid, String cname, String ename, String number, String type, String ccontacts, String econtacts, String phone, String mobile, String fax, String email, String addressCn, String addressEn, String remark) {
		SQLiteDatabase db = this.getWritableDatabase();
		String where = fid + "= ?";
		String[] wherevalues = { fid };
		ContentValues cv = new ContentValues();
		cv.put("cname", cname);
		cv.put("ename", ename);
		cv.put("number", number);
		cv.put("type", type);
		cv.put("ccontacts", ccontacts);
		cv.put("econtacts", econtacts);
		cv.put("phone", phone);
		cv.put("mobile", mobile);
		cv.put("fax", fax);
		cv.put("email", email);
		cv.put("addressCn", addressCn);
		cv.put("addressEn", addressEn);
		cv.put("remark", remark);
		db.update(MyApplication.getApplicationInstance().tableName, cv, where, wherevalues);
	}

}
